<?php

namespace App\Models;

use App\Models\Traits\Filters;
use Illuminate\Database\Eloquent\Model;

class RentCloth extends Model
{

    use Filters;

    protected $table = 'rent_clothes';

    // protected $fillable = [
    //     'shop_id', 'commodity_id', 'commodity_attr_id'
    // ];
    protected $guarded = [];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function commodity()
    {
        return $this->belongsTo(Commodity::class, 'commodity_id', 'id');
    }

    public function commodityattr()
    {
        return $this->belongsTo(CommodityAttr::class, 'commodity_attr_id', 'id');
    }

    public function shop()
    {
        return $this->belongsTo(Shop::class, 'shop_id', 'id');
    }

    public function rentclothdetail()
    {
        return $this->hasMany(RentClothDetail::class, 'rent_id', 'id'); //@TODO
    }

    public function logistics()
    {
        return $this->hasMany(LogisticsInfo::class, 'rent_id', 'id');
    }

    public function transform_lineBorrow_list()
    {
        $attr_id = $this->rentclothdetail->pluck('commodity_attr_id');

        $com_lists = CommodityAttr::whereIn('id', $attr_id)->with('commodity')->get();
        $com_lists->transform(function ($item) {
            $item->com_img = config('app.img_url') . ($item->commodity->pictures)[0];
            $item->com_name = $item->commodity->commodity_name;
            $item->com_desc = $item->commodity->commodity_desc;
            $item->attr = $item->attr_value;
            return $item->only(['com_img', 'com_name', 'com_desc', 'attr']);
        });

        switch ($this->status) {
            case '3': //待发货
                $status = '待发货';
                break;
            case '4': //待签收（已发货）
                $status = '待签收';
                break;
            case '5': //待归还（已收货）
                $status = '待归还';
                break;
            case '6': //已归还
                $status = '已归还';
                break;

            default:
                # code...
                break;
        }

        if (isset($this->logistics)) {
            $logistics_info = $this->logistics->transform(function ($item) {
                return [
                    'type' => $item->type,
                    'com_no' => $item->com_no,
                    'logistics_no' => $item->logistics_no,
                ];
            });
        } else {
            $logistics_info = [];
        }

        return [
            'id' => $this->id,
            'rent_id' => $this->id,
            'shop_id' => $this->shop_id,
            'com_lists' => $com_lists,
            'is_apply' => $this->is_apply,
            'code' => $this->status,
            'status' => $status,
            'is_apply' => $this->is_apply,
            'logistics_info' => $logistics_info,
        ];
    }
    public function transform_lineBuy_list()
    {
        $attr_id = $this->rentclothdetail->pluck('commodity_attr_id');
        $shop_id = $this->shop_id;

        $com_lists = CommodityAttr::whereIn('id', $attr_id)->with('commodity')->get();
        $com_lists->transform(function ($item) use ($shop_id) {
            $item->commodity_attr_id = $item->id;
            $item->com_img = config('app.img_url') . ($item->commodity->pictures)[0];
            $item->com_name = $item->commodity->commodity_name;
            $item->com_desc = $item->commodity->commodity_desc;
            $item->com_price = ShopCom::where(['shop_id' => $shop_id, 'commodity_attr_id' => $item->id, 'is_buy' => 1])->value('price');
            $item->attr = $item->attr_value;
            return $item->only(['commodity_attr_id','com_img', 'com_name', 'com_desc', 'com_price', 'attr']);
        });

        switch ($this->status) {
            case '3': //待发货
                $status = '待发货';
                break;
            case '4': //待签收（已发货）
                $status = '已发货';
                break;
            case '5': //待归还（已收货）
                $status = '已收货';
                break;
            case '8': //待支付
                $status = '待支付';
                break;
            case '7': //售后
                $status = '售后';
                break;

            default:
                $status = '未知状态';
                break;
        }
        if (isset($this->logistics)) {
            $logistics_info = $this->logistics->transform(function ($item) {
                return [
                    'type' => $item->type,
                    'com_no' => $item->com_no,
                    'logistics_no' => $item->logistics_no,
                ];
            });
        } else {
            $logistics_info = [];
        }

        return [
            'id' => $this->id,
            'rent_id' => $this->id,
            'shop_id' => $shop_id,
            'com_lists' => $com_lists,
            'total_price' => $this->total_price,
            'is_apply' => $this->is_apply,
            'code' => $this->status,
            'status' => $status,
            'is_apply' => $this->is_apply,
            'logistics_no' => $logistics_info,
        ];
    }

    public function transform_lineDetail()
    {
        $attr_id = $this->rentclothdetail->pluck('commodity_attr_id');
        $shop_id = $this->shop_id;
        $com_lists = CommodityAttr::whereIn('id', $attr_id)->with('commodity')->get();
        $com_lists->transform(function ($item) use ($shop_id) {
            $item->commodity_attr_id = $item->id;
            $item->com_img = config('app.img_url') . ($item->commodity->pictures)[0];
            $item->com_name = $item->commodity->commodity_name;
            $item->com_desc = $item->commodity->commodity_desc;
            $item->com_price = ShopCom::where(['shop_id' => $shop_id, 'commodity_attr_id' => $item->id, 'is_buy' => 1])->value('price');
            $item->attr = $item->attr_value;
            return $item->only(['commodity_attr_id','com_img', 'com_name', 'com_desc', 'com_price', 'attr']);
        });

        if (isset($this->logistics)) {
            $logistics_info = $this->logistics->transform(function ($item) {
                return [
                    'type' => $item->type,
                    'com_no' => $item->com_no,
                    'logistics_no' => $item->logistics_no,
                    'send_time' => ($item->send_time) ? date('Y-m-d H:i:s', $item->send_time) : '',
                    'receive_time' => ($item->receive_time) ? date('Y-m-d H:i:s', $item->receive_time) : '',
                ];
            });
        } else {
            $logistics_info = [];
        }
        switch ($this->status) {
            case '2': //已归还
                $status = '已归还';
                break;
            case '3': //待发货
                $status = '待发货';
                break;
            case '4': //待签收（已发货）
                $status = '待签收';
                break;
            case '5': //待归还（已收货）
                if($this->type == 1){
                    $status = '待归还';
                }
                if($this->type == 3){
                    $status = '已收货';
                }
                break;
            case '6': //已归还
                $status = '已归还';
                break;

            default:
                $status = '';
                break;
        }
        $return_time = $this->back_time ? date('Y-m-d H:i:s', $this->back_time) : 0;
        return [
            'rent_id' => $this->id,
            'shop_id' => $this->shop_id,
            'com_lists' => $com_lists,
            'create_time' => $this->created_at->toDateTimeString(),
            'return_time' => $return_time,
            'pay_time' => $this->pay_at,
            'is_apply' => $this->is_apply,
            'code' => $this->status,
            'status' => $status,
            'logistics_info' => $logistics_info,
            'pay_type' => $this->pay_type,
            'total_price' => $this->total_price,
            'total_e' => $this->total_e,
        ];
    }

    public function rentClothList()
    {
        return 111;
    }

    public function transform_user_record()
    {
        $details = $this->rentclothdetail;
        $cloths = [];

        return [
            'rent_time' => $this->created_at->toDateTimeString(),
            'rent_time' => $this->created_at->toDateTimeString(),
        ];
    }

}
